<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>水果店</title>
  <style>
    body {
      font-family: Arial, sans-serif;
    }

    .container {
      width: 60%;
      margin: 0 auto;
      border: 1px solid #000;
      padding: 10px;
      text-align: center;
    }

    h1 {
      font-size: 20px;
      font-weight: bold;
      margin: 20px 0;
    }

    h2 {
      font-size: 24px;
      margin: 10px 0;
    }

    input[type="text"] {
      width: 100px;
      padding: 5px;
      margin: 10px 0;
    }

    button {
      padding: 10px 20px;
      margin: 10px 0;
    }

    .result {
      border-top: 1px solid #000;
      padding: 10px;
      margin-top: 10px;
    }
  </style>
</head>

<body>

  <div id="app">
    <div class="container">
      <h1>欢迎光临 vue 开发的收银系统_水果店</h1>
      <h2>苹果 {{ price }} ¥ / 斤，折扣 <8 折>
      </h2>
      <label for="quantity">请输入你要购买的斤数：</label>
      <input type="number" v-model="num"><br>
      <button @click="buy">结账买单~</button>

      <!-- 结账单结果，初始显示为0 -->
      <div class="result">
        <p>结账单：总价:{{isClicked? discountPrice:0 }} ¥元 
          折后价: {{isClicked?totalPrice:0}}¥元 省了: {{ isClicked?savedAmount:0 }} ¥元</p>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        num: 1,         // 用户输入的数量
        price: 10,     // 单价
        discount: 0.8, // 折扣
        totalPrice: 0,
        isClicked: false, // 折后价，初始为0
      },
      computed: {
        discountPrice() {
          return this.num * this.price; // 计算未折扣的总价
        },
        savedAmount() {
          return this.discountPrice - this.totalPrice; // 计算省下的金额
        },
      },
      watch:{
        //输入框数据改变时，触发num的更新,isClicked的更新
        num:{
          handler(val){
           this.isClicked = false; // 输入框数据改变，折后价清零
          },
        }
      },
      methods: {
        buy() {
          this.isClicked = true; // 点击结账按钮，显示结账单
          this.totalPrice = this.discountPrice * this.discount; // 计算折后价
        },
        
       
      }
    });
  </script>

</body>

</html>